#include "stdafx.h"
#include "ScriptManager.h"

ScriptManager::ScriptManager()
{
}
ScriptManager::~ScriptManager()
{

}
Script* ScriptManager::Add(ScriptId id)
{
	return manager<Script,ScriptId>::Add(id);
}
bool ScriptManager::Remove(ScriptId id)
{
	Script* pScript = manager<Script,ScriptId>::Get(id);
	if (pScript != NULL)
	{
		std::map< std::string, Script* >::iterator iter = m_mapNameScripts.find(pScript->GetFileName()); 
		if (iter != m_mapNameScripts.end())
		{
			m_mapNameScripts.erase(iter);
		}
	}
	return manager<Script,ScriptId>::Remove(id);
}
void ScriptManager::Clear()
{
	manager<Script,ScriptId>::Clear();
	m_mapNameScripts.clear();
}
Script* ScriptManager::GetBattleScript(std::string strFileName,bool bReload)
{
	std::map< std::string, Script* >::iterator iter = m_mapNameScripts.find(strFileName); 
	if (iter != m_mapNameScripts.end())
	{
		if (iter->second == NULL)
		{
			m_mapNameScripts.erase(iter);
		}
		else
		{
			if (bReload)
			{
				Remove(iter->second->GetScriptId());
			}
			else
				return iter->second;
		}
	}

	Int32 nId = GenerateIdInt32();
	Script* pScript = Add(nId);
	if (pScript == NULL)
	{
		LOGGER_ERROR("GetByFileName,pScript == NULL,nId:%d",nId);
		return NULL;
	}
	RegisterBattleToScript(pScript);
	pScript->SetFileName(strFileName);
	if (!pScript->DoFile(strFileName))
	{
		Remove(nId);
		return NULL;
	}
	m_mapNameScripts[strFileName] = pScript;
	return pScript;
}

Script* ScriptManager::GetPlayerScript(std::string strFileName,bool bReload)
{
	std::map< std::string, Script* >::iterator iter = m_mapNameScripts.find(strFileName); 
	if (iter != m_mapNameScripts.end())
	{
		if (iter->second == NULL)
		{
			m_mapNameScripts.erase(iter);
		}
		else
		{
			if (bReload)
			{
				Remove(iter->second->GetScriptId());
			}
			else
				return iter->second;
		}
	}

	Int32 nId = GenerateIdInt32();
	Script* pScript = Add(nId);
	if (pScript == NULL)
	{
		LOGGER_ERROR("GetByFileName,pScript == NULL,nId:%d",nId);
		return NULL;
	}
	RegisterPlayerToScript(pScript);
	pScript->SetFileName(strFileName);
	if (!pScript->DoFile(strFileName))
	{
		Remove(nId);
		return NULL;
	}
	m_mapNameScripts[strFileName] = pScript;
	return pScript;
}
void ScriptManager::RegisterBattle(std::string strFunctionName, LuaFunction function)
{
	std::map< std::string, LuaFunction >::iterator iter = m_mapBattleFunctions.find(strFunctionName);
	if (iter != m_mapBattleFunctions.end())
	{
		LOGGER_ERROR("Register,iter != m_mapBattleFunctions.end(),strFunctionName:%s",strFunctionName.c_str());
		return;
	}
	m_mapBattleFunctions[strFunctionName] = function;
}
void ScriptManager::RegisterBattleToScript(Script* pScript)
{
	if (pScript == NULL) return;
	std::map< std::string, LuaFunction >::iterator iter = m_mapBattleFunctions.begin();
	std::map< std::string, LuaFunction >::iterator iterend = m_mapBattleFunctions.end();
	for (; iter != iterend; iter++)
	{
		pScript->RegisterFunction(iter->first,iter->second);
	}
}

void ScriptManager::RegisterPlayer(std::string strFunctionName, LuaFunction function)
{
	std::map< std::string, LuaFunction >::iterator iter = m_mapPlayerFunctions.find(strFunctionName);
	if (iter != m_mapPlayerFunctions.end())
	{
		LOGGER_ERROR("Register,iter != m_mapPlayerFunctions.end(),strFunctionName:%s",strFunctionName.c_str());
		return;
	}
	m_mapPlayerFunctions[strFunctionName] = function;
}
void ScriptManager::RegisterPlayerToScript(Script* pScript)
{
	if (pScript == NULL) return;
	std::map< std::string, LuaFunction >::iterator iter = m_mapPlayerFunctions.begin();
	std::map< std::string, LuaFunction >::iterator iterend = m_mapPlayerFunctions.end();
	for (; iter != iterend; iter++)
	{
		pScript->RegisterFunction(iter->first,iter->second);
	}
}

Script* ScriptManager::GetItemScript(Int32 csv_id)
{
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string strLuaFileName = path + "/lua/item/";
	char szPath[280] = "";
	sprintf(szPath,"item%05d.lua",csv_id);
	strLuaFileName += szPath;
	return GetPlayerScript(strLuaFileName);
}
Script* ScriptManager::GetSkillScript(Int32 csv_id)
{
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string strLuaFileName = path + "/lua/skill/";
	char szPath[280] = "";
	sprintf(szPath,"skill%05d.lua",csv_id);
	strLuaFileName += szPath;
	return GetBattleScript(strLuaFileName);
}
Script* ScriptManager::GetBuffScript(Int32 csv_id)
{
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string strLuaFileName = path + "/lua/buff/";
	char szPath[280] = "";
	sprintf(szPath,"buff%05d.lua",csv_id);
	strLuaFileName += szPath;
	return GetPlayerScript(strLuaFileName);
}